package org.rcy.framework.data.config.mybatis;


import org.rcy.framework.data.dao.MapperHelperFactory;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.ClassPathMapperScanner;

import java.util.Set;

public class MybatisMapperScanner extends ClassPathMapperScanner {

	private boolean lazyInitialization;

	public MybatisMapperScanner(BeanDefinitionRegistry registry) {
		super(registry);
		MapperHelper mapperHelper = MapperHelperFactory.getMapperHelper();
		this.setMapperHelper(mapperHelper);
	}

	@Override
	public Set<BeanDefinitionHolder> doScan(String... basePackages) {
		Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

		for (BeanDefinitionHolder holder : beanDefinitions) {
			GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
			definition.setLazyInit(lazyInitialization);
		}
		return beanDefinitions;
	}

	/**
	 * Set whether enable lazy initialization for mapper bean.
	 * <p>
	 * Default is {@code false}.
	 * </p>
	 *
	 * @param lazyInitialization Set the @{code true} to enable
	 * @since 2.0.2
	 */
	public void setLazyInitialization(boolean lazyInitialization) {
		this.lazyInitialization = lazyInitialization;
	}

}
